//
//  BiAssociateCheckViewController.m
//  TrunkIndustryManageSystem
//
//  Created by WT_lyy on 15/4/22.
//  Copyright (c) 2015年 李璐西. All rights reserved.
//

#import "BiAssociateCheckViewController.h"

@interface BiAssociateCheckViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>{

    __weak IBOutlet UIScrollView *_scrollView;
    __weak IBOutlet UILabel *_labVip;
    
    __weak IBOutlet UILabel *_labTel;
    __weak IBOutlet UILabel *_labnation;
    __weak IBOutlet UILabel *_labAddress;
    __weak IBOutlet UILabel *_labVin;
    __weak IBOutlet UILabel *_labSalePrice;
    __weak IBOutlet UILabel *_laboffcount;
    __weak IBOutlet UILabel *_labneedAccept;
    __weak IBOutlet UILabel *_labBalance;
    __weak IBOutlet UILabel *_labRealAcctp;
    __weak IBOutlet UILabel *_labsaler;
    __weak IBOutlet UILabel *_labordertime;
    
    __weak IBOutlet UIImageView *_imageViewWPJJD;
    
    __weak IBOutlet UIImageView *_imageViewCLJJD;
    __weak IBOutlet UIImageView *_imageViewRCHY;
    
    __weak IBOutlet UILabel *_labHandler;
    __weak IBOutlet UILabel *_labHandleTime;
    NSInteger imageType;  //1wpjjd 2cljjd 3 rchy
    
    NSInteger countIm;  //图片需上传计数
    NSInteger countUp; //图片上传完成计数
    
    BOOL isSave;
}

@property (nonatomic,strong) UIImage * imageWPJJD;
@property (nonatomic,strong) UIImage * imageCLJJD;
@property (nonatomic,strong) UIImage * imageRCHY;

@end

@implementation BiAssociateCheckViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    isSave = NO;
    countIm = 0;
    countUp = 0;
    
    [self setNavBarTitle:@"车辆交接"];
    
    [self setNavBarWithHidden:NO];
    
    [self setNavBackBtn];
    
    [self dataFill];
}

- (void)dataFill{

    _labVip.text = self.Centity.member_name;
    
    _labTel.text = self.Centity.member_phone;
    _labnation.text = self.Centity.member_nation;
    _labAddress.text = self.Centity.member_address;
    _labVin.text = self.Centity.car_vin;
    _labSalePrice.text = self.Centity.carOrder_sellPrice;
    _laboffcount.text = [NSString stringWithFormat:@"%0.2f",self.Centity.carOrder_discountMoney];
    _labneedAccept.text = [NSString stringWithFormat:@"%0.2f",self.Centity.carOrder_yingshouMoney];
    _labBalance.text = [NSString stringWithFormat:@"%0.2f",self.Centity.carOrder_balanceMoney];
    _labRealAcctp.text = [NSString stringWithFormat:@"%0.2f",self.Centity.carOrder_yingshouMoney];
    _labsaler.text = self.Centity.saleman_name;
    _labordertime.text = self.Centity.carOrder_orderTime;
    
    _imageViewCLJJD.image = LOAD_IMAGE_WITH_CACHE(@"CarSale/default");
    _imageViewWPJJD.image = LOAD_IMAGE_WITH_CACHE(@"CarSale/default");
    _imageViewRCHY.image = LOAD_IMAGE_WITH_CACHE(@"CarSale/default");
    
    _labHandleTime.text = self.Centity.carOrder_connectTime;
    
    NSString * delivier = self.Centity.connectUser_name.length==0?[BiUserEntity sharedInstance].name:self.Centity.connectUser_name;
    
    _labHandler.text = delivier;
    
    if (self.Centity.carOrder_articleConnectImg.length != 0) {
        
        [_imageViewWPJJD reloadImage:self.Centity.carOrder_articleConnectImg];
    }
    if (self.Centity.carOrder_groupPhotoImg.length != 0) {
        
        [_imageViewRCHY reloadImage:self.Centity.carOrder_groupPhotoImg];
    }
    if (self.Centity.carOrder_carConnectImg.length != 0) {
        
        [_imageViewCLJJD reloadImage:self.Centity.carOrder_carConnectImg];
    }

    _scrollView.contentSize = CGSizeMake(MAIN_SCREEN_WIDTH, _scrollView.frame.size.height);
    
    if (isIPhone4) {
        _scrollView.contentSize = CGSizeMake(MAIN_SCREEN_WIDTH, 500);

    }
   
}


- (IBAction)btnSaveCheckClick:(UIButton *)sender {
    
    if (0 == sender.tag) {
        //save
        NSDictionary * paraDic = [NSDictionary dictionaryWithObjectsAndKeys:self.Centity.carOrder_id,@"id",[BiUtil stringFromFomate:[NSDate date] formate:@"yyyy-MM-dd hh:ss"],@"connectTime",self.Centity.carOrder_id ,@"id", nil];
        
        [[BiNetEngine shareInstance] requestSaleContractAssocSave:self paraDict:paraDic];

        
    }else if (1 == sender.tag){
        //check
        NSDictionary * paraDic = [NSDictionary dictionaryWithObjectsAndKeys:self.Centity.carOrder_id,@"id",@"1",@"status", nil];
        
        [[BiNetEngine shareInstance] requestSaleContractAssocCheck:self paraDict:paraDic];
    }
    
    
    
    NSString * filename = nil;
    NSDictionary * paraImgDic = nil;
    if (self.imageWPJJD) {
        filename = [NSString stringWithFormat:@"%u",arc4random()];
        paraImgDic = [NSDictionary dictionaryWithObjectsAndKeys:self.Centity.carOrder_id,@"id",@"3",@"imgType",filename,@"fileName", nil];
        
        [[BiNetEngine shareInstance] requestSaleContractImageWPJJD:self withImge:self.imageWPJJD paraDic:paraImgDic];
    }
    if (self.imageCLJJD) {
        filename = [NSString stringWithFormat:@"%u",arc4random()+1];
        paraImgDic = [NSDictionary dictionaryWithObjectsAndKeys:self.Centity.carOrder_id,@"id",@"2",@"imgType",filename,@"fileName", nil];
        
        [[BiNetEngine shareInstance] requestSaleContractImageCLJJD:self withImge:self.imageCLJJD paraDic:paraImgDic];
    }
    if (self.imageRCHY) {
        filename = [NSString stringWithFormat:@"%u",arc4random()+2];
        paraImgDic = [NSDictionary dictionaryWithObjectsAndKeys:self.Centity.carOrder_id,@"id",@"4",@"imgType",filename,@"fileName", nil];
        
        [[BiNetEngine shareInstance] requestSaleContractImageRCHY:self withImge:self.imageRCHY paraDic:paraImgDic];
    }
    
      [self asyshowHUDView:WAITING CurrentView:self];
}

- (IBAction)btnImageClick:(UIButton *)sender {
    
    imageType = sender.tag - 10;
    
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"相机功能不可用" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alertView show];
        return;
    }
    
    UIImagePickerController * picker = [[UIImagePickerController alloc]init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self presentViewController:picker animated:YES completion:nil];
}


#pragma mark UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    image = [BiUtil scaleToSize:image size:CGSizeMake(120, 100)];
    if (1 == imageType) {
        
        _imageViewWPJJD.image = image;
        
        self.imageWPJJD = image;
    }else if (2 == imageType){
        _imageViewCLJJD.image = image;
        self.imageCLJJD = image;
    }else if (3 == imageType){
        _imageViewRCHY.image = image;
        self.imageRCHY = image;
    }
    
    countIm ++;
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}


#pragma -mark Net Delegate
- (void)onNetEngineResponse:(int)tag entity:(id)entity userInfo:(NSDictionary *)userInfo
{
    
    NSDictionary * entity_ = (NSDictionary *)entity;
    
    [self showMessage:entity_[@"result_info"]];
    
    if ([entity_[@"response_status"] integerValue] != 200) {
        
        [self removeHUDView:self];
        
        return;
        
    }
    
    switch (tag) {
        case NET_GET_XINGXIAO_CONTRACT_CHECK:
            isSave = YES;
            break;
        case NET_GET_XINGXIAO_CONTRACT_SAVE:
            isSave = YES;
            break;
        case NET_GET_XINGXIAO_CONTRACT_UPIMAGEWPJJD:
            countUp++;
            break;
        case NET_GET_XINGXIAO_CONTRACT_UPIMAGECLJJD:
            countUp++;
            break;
        case NET_GET_XINGXIAO_CONTRACT_UPIMAGERCHY:
            countUp++;
            break;
        default:
            break;
    }
    
    if (countUp == countIm  && isSave) {
        
        [self removeHUDView:self];
        
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)onNetEngineResponseFail:(int)tag entity:(id)entity userInfo:(NSDictionary *)userInfo
{
    [self removeHUDView:self];
}


@end
